package com.jy.adscm.operation.mask.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.mask.bean.KzMaterial;
import com.jy.adscm.operation.mask.bean.KzMaterialDetail;
import com.jy.adscm.operation.mask.bean.QueryKzMaterialDetail;
import com.jy.adscm.operation.mask.mapper.mysql.KzMaterialDetailMapper;
import com.jy.adscm.operation.mask.mapper.mysql.KzMaterialMapper;
import com.jy.adscm.operation.mask.service.KzMaterialDetailService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2020-03-24
 */
@Service
public class KzMaterialDetailServiceImpl extends ServiceImpl<KzMaterialDetailMapper, KzMaterialDetail> implements KzMaterialDetailService {
    @Value("${mask.print}")
   private String maskPrint;
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    KzMaterialMapper kzMaterialMapper;
    @Override
    public  Page<KzMaterialDetail> selectByPid(QueryKzMaterialDetail queryKzMaterialDetail) {
        Page<KzMaterialDetail> page=new Page<>(queryKzMaterialDetail.getCurrentPage(),queryKzMaterialDetail.getPageSize());
        Wrapper<KzMaterialDetail> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryKzMaterialDetail.getCode(),null)){
            wrapper.eq("code",queryKzMaterialDetail.getCode());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryKzMaterialDetail.getResult(),null)){
            wrapper.isNotNull("result");
        }else {
            wrapper.isNull("result");
        }
        wrapper.eq("pid",queryKzMaterialDetail.getPid());
        wrapper.orderBy("create_time",false);
        page= super.selectPage(page,wrapper);
        List<KzMaterialDetail> kzMaterialDetails = page.getRecords();
        for (KzMaterialDetail kzMaterialDetail : kzMaterialDetails) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(kzMaterialDetail.getOutStocker(),null)){
                JyUser jyUser = jyUserMapper.selectById(kzMaterialDetail.getOutStocker());
                kzMaterialDetail.setOutStockerName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(kzMaterialDetail.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(kzMaterialDetail.getCreator());
                kzMaterialDetail.setCreatorName(jyUser.getName());
            }
        }
        page.setTotal(selectCount(wrapper));
        return page;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateKzMaterialDetail(Token token, KzMaterialDetail kzMaterialDetail) {
        KzMaterialDetail oldKzMaterialDetail = super.selectById(kzMaterialDetail.getId());
        if (AdscmBaseUtils.getInstance().isNotEmpty(kzMaterialDetail.getWeight(),null)){
            if (!AdscmBaseUtils.getInstance().isNotEmpty(oldKzMaterialDetail.getWeight(),null)){
                kzMaterialDetail.setCreator(token.getUser().getId().toString());
            }
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(kzMaterialDetail.getOutStockDate(),null)){
            if (!AdscmBaseUtils.getInstance().isNotEmpty(oldKzMaterialDetail.getOutStockDate(),null)){
                kzMaterialDetail.setOutStocker(token.getUser().getId().toString());
            }
        }
        boolean b = super.updateById(kzMaterialDetail);

        return b;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public KzMaterialDetail insertkzMaterialDetail(KzMaterialDetail kzMaterialDetail) {
         synchronized (this){
             String date =new SimpleDateFormat("yyyyMMdd").format(new Date());
             Wrapper<KzMaterialDetail> wrapper=new EntityWrapper<>();
             wrapper.like("code",date, SqlLike.RIGHT);
             wrapper.orderBy("code",false);
             wrapper.last(" limit 1");
             List<KzMaterialDetail> kzMaterialDetails = super.selectList(wrapper);
             String  code ="";
             if(kzMaterialDetails!=null&& kzMaterialDetails.size()>0){
                 KzMaterialDetail old = kzMaterialDetails.get(0);
                 code = String.valueOf(Long.valueOf(old.getCode())+1) ;
             }else {
                 code=date+"0001";
             }
             kzMaterialDetail.setCode(code);
             boolean insert = super.insert(kzMaterialDetail);
             return kzMaterialDetail;
         }
    }
    @Async("asyncServiceExecutor")
    @Override
    public void printCode(String code) {
        HttpRequest.sendPost("http://"+maskPrint+"/javaPrint/qrCodePrint","qrCode="+code);
    }





    @Override
    public Boolean outPutMaterial(String code, Integer result,Token token) {
        Wrapper<KzMaterialDetail> kzMaterialDetailWrapper=new EntityWrapper<>();
        kzMaterialDetailWrapper.eq("code",code);
        List<KzMaterialDetail> kzMaterialDetails = super.selectList(kzMaterialDetailWrapper);
        if (kzMaterialDetails!=null&&kzMaterialDetails.size()>0){
            KzMaterialDetail kzMaterialDetail = kzMaterialDetails.get(0);
            kzMaterialDetail.setOutStocker(token.getUser().getId().toString());
            kzMaterialDetail.setOutStockDate(new Date());
            kzMaterialDetail.setResult(result);
            super.updateById(kzMaterialDetail);
        }else {
            return false;
        }
        return true;
    }

    @Override
    public String getMaxCode() {
        String date =new SimpleDateFormat("yyyyMMdd").format(new Date());
        Wrapper<KzMaterialDetail> wrapper=new EntityWrapper<>();
        wrapper.like("code",date, SqlLike.RIGHT);
        wrapper.orderBy("code",false);
        wrapper.last(" limit 1");
        List<KzMaterialDetail> kzMaterialDetails = super.selectList(wrapper);
        String  code ="";
        if(kzMaterialDetails!=null&& kzMaterialDetails.size()>0){
            KzMaterialDetail old = kzMaterialDetails.get(0);
            code = String.valueOf(Long.valueOf(old.getCode())+1) ;
        }else {
            code=date+"0001";
        }
        return code;
    }


}